package com.fencing_squad.homo.slice;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fencing_squad.homo.ResourceTable;
import com.fencing_squad.homo.list.Post;
import com.fencing_squad.homo.list.PostUser;
import com.fencing_squad.homo.list.PostUserCard;
import com.fencing_squad.homo.list.PostUserProvider;
import com.fencing_squad.homo.util.CustomComponent;
import com.fencing_squad.homo.util.Data;
import com.fencing_squad.homo.util.Network;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;

import java.util.ArrayList;

public class UserPostSlice extends AbilitySlice {
    PostUserProvider provider;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent((ComponentContainer) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_user_post_page, null, false));

        initConfig();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    private void initConfig() {
        ListContainer container = (ListContainer) findComponentById(ResourceTable.Id_userPostList);

        Text barTitle = (Text) findComponentById(ResourceTable.Id_barTitle);
        Component barBack = findComponentById(ResourceTable.Id_barBack);
        barTitle.setText("我的帖子");
        barBack.setClickedListener(component -> {
            terminate();
        });

        provider = new PostUserProvider(this);
        container.setItemProvider(provider);
        container.setItemClickedListener((listContainer, component, i, l) -> {
            if (i == 0) {
                return;
            }
            PostUser postUser = (PostUser) provider.getItem(i);
            Intent intent = new Intent();
            intent.setParam("post_message_id", postUser.getPostMessageId());
            present(new PostDetailSlice(), intent);
        });

        Network.get("/user/post_message", this::resolve, this::reject);
    }

    private void resolve(JSONObject object) {
        JSONArray data = object.getJSONObject("data").getJSONArray("data");
        JSONArray sum = object.getJSONObject("data").getJSONArray("sum");
        PostUserCard postUserCard = new PostUserCard();
        postUserCard.setNickname(Data.Account.nickname);
        postUserCard.setAvatar(Data.Account.avatar);
        postUserCard.setPost(sum.getIntValue(0));
        postUserCard.setComment(sum.getIntValue(1));
        postUserCard.setLikes(sum.getIntValue(2));
        provider.setPostUserCard(postUserCard);
        provider.append(PostUser.getFromJSONArray(data));
    }

    private void reject(int code, String message) {
        if(code == 404) {
            CustomComponent.getDialog(this, message, null, null).show();
        }
        else {
            System.out.println(message);
        }
    }

    public static ArrayList<PostUser> test() {
        ArrayList<PostUser> list = new ArrayList<>();
        for(int i=0; i<10; i++) {
            PostUser post = new PostUser();
            post.setContent("contentttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt" + String.valueOf(i));
            post.setComment(i);
            post.setLikes(i);
            post.setNickname("nick" + String.valueOf(i));
            post.setDate("7月"+String.valueOf(i)+"日");
            list.add(post);
        }
        return list;
    }
}
